package uk.co.BexhillWeb.MediaManager.Managers;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.logging.Logger;

import uk.co.BexhillWeb.MediaManager.Core;
import uk.co.BexhillWeb.MediaManager.PluginSystem.PluginFactory;
import uk.co.BexhillWeb.MediaManager.PluginSystem.PluginInterface;

@SuppressWarnings("rawtypes")
public class PluginManager {

	public PluginManager() {
		PluginInterface pluginService = PluginFactory.createPluginService();
        pluginService.initPlugins();
	}
	
	private static Logger logger = Logger.getLogger(Core.class.getName());
	 // Parameters
	 
	private static final Class[] parameters = new Class[]
	 {
	     URL.class
	 };

	 public static void addDirToClasspath(File directory) throws IOException
	 {
	     if (directory.exists())
	     {
	         File[] files = directory.listFiles();
	         for (int i = 0; i < files.length; i++)
	         {
	             File file = files[i];
	             addURL(file.toURI().toURL());
	         }
	     }
	     else
	     {
	         logger.warning("The directory \"" + directory + "\" does not exist!");
	     }
	}
	 
	 public static void addURL(URL u) throws IOException
	 {
	     URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
	     URL urls[] = sysLoader.getURLs();
	     for (int i = 0; i < urls.length; i++)
	     {
	         if (urls[i].toString().equalsIgnoreCase(u.toString()))
	         {
	             logger.info("URL " + u + " is already in the CLASSPATH");
	             return;
	         }
	     }
	     Class sysclass = URLClassLoader.class;
	     try
	     {
	         Method method = sysclass.getDeclaredMethod("addURL", parameters);
	         method.setAccessible(true);
	         method.invoke(sysLoader, new Object[]
	         {
	             u
	         });
	     } catch (Throwable t)
	     {
	         t.printStackTrace();
	         throw new IOException("Error, could not add URL to system classloader");
	     }
	 }
}
